package com.keepers.repair.view.picker;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.contrarywind.view.WheelView;
import com.keepers.repair.R;
import com.keepers.repair.utils.DialogUtils;
import com.keepers.repair.view.picker.adapter.ArrayWheelAdapter;
import com.keepers.repair.view.picker.info.PickerInfo;

import java.util.ArrayList;
import java.util.List;


/**
 * auth aboom
 * date 2018/8/27
 */
public class SinglePickerDialog extends Dialog {

    private List<PickerInfo> pickers;
    private PickerInfo       chooseData;
    private OnClickListener  listener;

    private WheelView wheelView;
    private TextView  pickerTitleView;
    private TextView  extensionView;

    private boolean isNullCount;

    public SinglePickerDialog(@NonNull Context context) {
        super(context, R.style.rest_widget_Dialog_Tip);
        setContentView(R.layout.view_single_picker);
        initViews();
    }

    private void initViews() {
        pickerTitleView = findViewById(R.id.picker_title);
        findViewById(R.id.btn_cancel).setOnClickListener(v -> cancel());
        findViewById(R.id.btn_confirm).setOnClickListener(v -> confirm());
        extensionView = findViewById(R.id.btn_extension);
        wheelView = findViewById(R.id.wheel_single_line);
        wheelView.setCyclic(Boolean.FALSE);
        wheelView.setOnItemSelectedListener(index -> chooseData = pickers.get(index));
        wheelView.setTextSize(18);
    }

    public SinglePickerDialog setPickerTitle(String pickerTitle) {
        pickerTitleView.setText(pickerTitle);
        return this;
    }

    public SinglePickerDialog setPickers(List<PickerInfo> pickers) {
        this.pickers = pickers;
        this.isNullCount = Boolean.FALSE;
        if (this.pickers == null || this.pickers.size() == 0) {
            this.pickers = new ArrayList<>();
            this.pickers.add(new PickerInfo(""));
            this.isNullCount = Boolean.TRUE;
        }
        wheelView.setAdapter(new ArrayWheelAdapter<>(this.pickers));
        return this;
    }

    public SinglePickerDialog setExtension(String extension, View.OnClickListener onClickListener) {
        extensionView.setText(extension);
        extensionView.setVisibility(TextUtils.isEmpty(extension) ? View.GONE : View.VISIBLE);
        extensionView.setOnClickListener(v -> {
            if (onClickListener != null) {
                onClickListener.onClick(v);
            }
            cancel();
        });
        return this;
    }

    /**
     * 要在setPickers后调用
     */
    public SinglePickerDialog setChoose(String chooseData) {
        this.chooseData = new PickerInfo(chooseData);
        wheelView.setCurrentItem(getPosition(pickers, this.chooseData));
        return this;
    }

    public SinglePickerDialog setChoose(PickerInfo chooseData) {
        wheelView.setCurrentItem(getPosition(pickers, chooseData));
        this.chooseData = chooseData;
        return this;
    }

    public SinglePickerDialog setCyclic(boolean cyclic) {
        wheelView.setCyclic(cyclic);
        return this;
    }

    public SinglePickerDialog setListener(OnClickListener listener) {
        this.listener = listener;
        return this;
    }

    public interface OnClickListener {
        void onItemClick(int position, PickerInfo pickerData);
    }

    private void confirm() {
        if (listener != null && !isNullCount) {
            listener.onItemClick(wheelView.getCurrentItem(), pickers.get(wheelView.getCurrentItem()));
        }
        this.dismiss();
    }

    private int getPosition(List<PickerInfo> pickerDataList, PickerInfo pickerData) {
        int position = 0;
        for (int i = 0; i < pickerDataList.size(); i++) {
            if (pickerDataList.get(i).equals(pickerData)) {
                position = i;
                break;
            }
        }
        return position;
    }

    @Override
    public void show() {
        if (!DialogUtils.activityIsRunning(getContext())) {
            return;
        }
        super.show();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        configDialog();
    }

    /**
     * 设置dialog属性
     */
    private void configDialog() {
        Window dialogWindow = getWindow();
        if (dialogWindow != null) {
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            dialogWindow.setGravity(Gravity.BOTTOM);
            dialogWindow.setWindowAnimations(R.style.rest_widget_anim_slide_share_from_bottom);
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        }
        setCancelable(true);
    }
}
